/**
 * @format
 * @Author: TC
 * @Date: 2023-03-25 14:39:17
 * @Last Modified by: TC
 * @Last Modified time: 2025-05-05 01:53:18
 */

import { Button, EventHandler, EventTouch } from "cc";

/**
 * 定制引擎按钮事件监听
 */
export namespace nsCCButton {
    Button.prototype["onLoad"] = function () {
        const self = this as Button;
        if (Button.Transition.SCALE == self.transition) {
            self.zoomScale = 0.9;
        }
    };
    //@ts-ignore
    Button.prototype._onTouchBegan = function (event?: EventTouch) {
        if (!this._interactable || !this.enabledInHierarchy) {
            return;
        }
        this._pressed = true;
        this._updateState();
        if (event) {
            event.propagationStopped = true;
        }
    };

    //@ts-ignore
    Button.prototype._onTouchEnded = function (event?: EventTouch) {
        if (!this._interactable || !this.enabledInHierarchy) {
            return;
        }
        if (this._pressed) {
            //@ts-ignore
            EventHandler.emitEvents(this.clickEvents, event);
            this.node.emit("click", this);
        }
        this._pressed = false;
        this._updateState();

        if (event) {
            event.propagationStopped = true;
        }
        // console.log("end"+this.node.name)
    };

    //@ts-ignore
    Button.prototype._onTouchCancel = function (event?: EventTouch) {
        if (!this._interactable || !this.enabledInHierarchy) {
            return;
        }
        this._pressed = false;
        this._updateState();
    };
}
